import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:lx_widgets_library/lx_widgets_library.dart';

typedef SelectedCallback = void Function(int selectIndex, String? selectString);

class LxBottomSheet {
  // 弹出底部sheet
  static void showBody(
    BuildContext context, {
    required Widget child,
    Color? backgroundColor,
    double? height,
    bool isScrollControlled = false,
  }) {
    showModalBottomSheet(
      context: context,
      isScrollControlled: isScrollControlled,
      backgroundColor: backgroundColor ?? GrayColor.gray2,
      builder: (BuildContext context) {
        return Container(
          height: height ?? 300.w,
          decoration: BoxDecoration(
            color: backgroundColor ?? GrayColor.gray2,
            borderRadius: BorderRadius.vertical(top: Radius.circular(16.r)),
          ),
          child: child,
        );
      },
    );
  }

  // 弹出Slider选择器
  static void showSlider(
    BuildContext context, {
    required String title,
    String? unit,
    required double min,
    required double max,
    required RxDouble value,
    ValueChanged<double>? onChanged,
    ValueChanged<double>? onChangeEnd,
    Color? backgroundColor,
    double? height,
  }) {
    showBody(
      context,
      backgroundColor: backgroundColor ?? GrayColor.gray2,
      height: height ?? (200.w + tabBarSpace),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Container(
            padding: EdgeInsets.only(top: CONTENT_MARGIN * 2),
            child: LxText(
              title,
              fontWeightEnum: LxFontWeightEnum.bodyMedium,
              color: Colors.white,
            ),
          ),
          Expanded(
            child: Padding(
              padding: EdgeInsets.symmetric(horizontal: CONTENT_MARGIN * 2),
              child: Column(
                mainAxisSize: MainAxisSize.max,
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Container(
                    padding: EdgeInsets.only(bottom: CONTENT_MARGIN / 2),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        LxText(
                          "${min.round()} ${unit ?? ""}",
                          fontWeightEnum: LxFontWeightEnum.bodyMedium,
                          color: Colors.white.withOpacity(0.8),
                        ),
                        LxText(
                          "${max.round()} ${unit ?? ""}",
                          fontWeightEnum: LxFontWeightEnum.bodyMedium,
                          color: Colors.white.withOpacity(0.8),
                        ),
                      ],
                    ),
                  ),
                  LxSlider(
                    min: min,
                    max: max,
                    value: value.value,
                    onChanged: (newValue) {
                      if (onChanged != null) {
                        onChanged(newValue);
                      }
                    },
                    onChangeEnd: (newValue) {
                      if (onChangeEnd != null) {
                        onChangeEnd(newValue);
                        Navigator.pop(context);
                      }
                    },
                  ),
                  SizedBox(height: CONTENT_MARGIN),
                  Center(
                    child: Obx(
                      () => LxText(
                        "${value.round()} ${unit ?? ""}",
                        fontWeightEnum: LxFontWeightEnum.titleMedium,
                        color: LxCol.theme,
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }

  // 弹出底部sheet
  static void show(
    BuildContext context, {
    required List<String> dataArr,
    String? title,
    int? selectIndex,
    SelectedCallback? selectedCallback,
    bool showCancel = false,
    String cancelTitle = "取消",
    LxFontWeightEnum titleFontWeight = LxFontWeightEnum.bodySmall,
    LxFontWeightEnum contentFontWeight = LxFontWeightEnum.bodyLarge,
    LxFontWeightEnum cancelFontWeight = LxFontWeightEnum.bodyMedium,
    Color titleColor = GrayColor.gray5,
    Color contentColor = WhiteColor.white1,
    Color? cancelColor,
    Color backgroundColor = GrayColor.gray2,
    Color? spaceColor,
    double? titleHeight,
    double? cellHeight,
    double? cancelHeight,
    double? cellLineHeight,
    double? spaceHeight,
    ScrollPhysics? physics,
    IconData? targetIconData,
    int? targetIndex,
  }) {
    bool existTitile = title != null && title.isNotEmpty;
    spaceColor ??= GrayColor.gray3.withOpacity(0.3);
    cancelColor ??= WhiteColor.white1.withOpacity(0.3);
    titleHeight ??= existTitile ? 56.0 : 0.0;
    double titleLineHeight = existTitile ? 1.0 : 0;
    cellHeight ??= 56.0;
    cancelHeight ??= 56.0;
    cellLineHeight ??= 0.5;
    spaceHeight ??= 5.0;
    physics ??= const NeverScrollableScrollPhysics();

    showModalBottomSheet(
      enableDrag: false,
      isScrollControlled: true,
      context: context,
      builder: (BuildContext context) {
        return SafeArea(
          child: Container(
            decoration: BoxDecoration(
              color: backgroundColor,
              borderRadius: const BorderRadius.only(
                topLeft: Radius.circular(10.0),
                topRight: Radius.circular(10.0),
              ),
            ),
            height: titleHeight! +
                titleLineHeight +
                (cellHeight! + cellLineHeight!) * dataArr.length +
                spaceHeight! +
                cancelHeight!,
            child: Column(
              children: <Widget>[
                // 标题
                Visibility(
                  visible: existTitile,
                  child: SizedBox(
                    height: titleHeight,
                    child: Center(
                      child: Text(
                        title ?? "",
                        style: TextStyle(
                            fontSize: titleFontWeight.size,
                            fontWeight: titleFontWeight.fontWeight,
                            color: titleColor),
                        textAlign: TextAlign.center,
                      ),
                    ),
                  ),
                ),
                SizedBox(
                  height: titleLineHeight,
                  child: Container(color: spaceColor),
                ),
                // 选择列表
                ListView.separated(
                  physics: physics,
                  itemCount: dataArr.length,
                  shrinkWrap: true,
                  itemBuilder: (BuildContext context, int index) {
                    return Material(
                      color: backgroundColor,
                      child: ListTile(
                        onTap: () {
                          Navigator.of(context).pop(index);
                          if (selectedCallback != null) {
                            selectedCallback(index, dataArr[index]);
                          }
                        },
                        title: Container(
                          constraints: BoxConstraints(minHeight: 30.w),
                          child: Stack(
                            alignment: Alignment.center,
                            children: [
                              LxText(
                                dataArr[index],
                                fontWeightEnum: contentFontWeight,
                                color: selectIndex == index
                                    ? LxCol.theme
                                    : contentColor,
                                textAlign: TextAlign.center,
                              ),
                              if (selectIndex == index)
                                Positioned(
                                  right: 0,
                                  child: Icon(
                                    (targetIndex != null &&
                                            targetIconData != null &&
                                            index == targetIndex)
                                        ? targetIconData
                                        : CupertinoIcons.checkmark,
                                    size: 20.w,
                                    color: LxCol.theme,
                                  ),
                                )
                            ],
                          ),
                        ),
                      ),
                    );
                  },
                  separatorBuilder: (context, index) {
                    return Divider(
                      height: cellLineHeight,
                      color: spaceColor,
                    );
                  },
                ),
                // 空白
                SizedBox(
                    height: spaceHeight, child: Container(color: spaceColor)),
                // 取消
                Material(
                  color: backgroundColor,
                  child: ListTile(
                    title: Center(
                      child: Text(
                        cancelTitle,
                        style: TextStyle(
                          fontSize: cancelFontWeight.size,
                          fontWeight: cancelFontWeight.fontWeight,
                          color: cancelColor,
                        ),
                        textAlign: TextAlign.center,
                      ),
                    ),
                    onTap: () {
                      Navigator.of(context).pop();
                    },
                  ),
                ),
              ],
            ),
          ),
        );
      },
    );
  }
}
